//
//  ChangeNameViewController.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/14.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "ChangeNameViewController.h"
#import "AFNetworking.h"//主要用于网络请求方法
#import "UIKit+AFNetworking.h"//里面有异步加载图片的方法
#import "AFHTTPSessionManager.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "NavView.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750

@interface ChangeNameViewController ()<UITextFieldDelegate>
@property (nonatomic,retain)UITextField *textField;
@end

@implementation ChangeNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"修改昵称"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
    UIButton *buttonNotice = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNotice.frame = CGRectMake(0, 0, 40, 20);
    buttonNotice.titleLabel.font = [UIFont systemFontOfSize:15];
    [buttonNotice setTitle:@"确定" forState:UIControlStateNormal];
    
    UIBarButtonItem *btnNoticeItem = [[UIBarButtonItem alloc]initWithCustomView:buttonNotice];
    [buttonNotice addTarget:self action:@selector(changeClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = btnNoticeItem;
    self.view.backgroundColor = [UIColor colorWithRed:32/255.0 green:32/255.0 blue:32/255.0 alpha:1.0f];
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    backgroundView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0f];
    [self.view addSubview:backgroundView];
    _textField = [[UITextField alloc]init];
    _textField.delegate = self;
    if (_user.name) {
        _textField.text = _user.name;
    }
    _textField.placeholder = @"给自己起个响亮的名字吧～";
    _textField.font = [UIFont systemFontOfSize:15];
    _textField.frame = CGRectMake(0, 0, kScreenW, 100*kScale);
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.clearButtonMode=UITextFieldViewModeWhileEditing;
    _textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30*kScale, 0)];
    //设置显示模式为永远显示(默认不显示)
    _textField.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:_textField];


}

- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)changeClick
{
    [self request];
}
- (void)request
{
    if (_textField.text.length!=0) {
        NSString *urlStr = [[NSString stringWithFormat:@"%@/userCenter/modifyMemberNickName?nick_name=%@",APIYIGOU,_textField.text]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        DYBaseRequest *request = [DYBaseRequest new];
        request.requestUrl = urlStr;
        [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
            NSLog(@"1111111%@",allDic);
            [self.navigationController popViewControllerAnimated:YES];
        } error:^(YTKBaseRequest *request) {
            DYLog(@"%@",request);
        } failure:^(YTKBaseRequest *request) {
            [ViewFactory showErrorStrMsg:NETWORK_ERR];
        }];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
